var douban=getApp().globalData.doubanUrl;
import utils from '../../utils/util';
var http=utils.http;
var star=utils.star;
var sliceTitle=utils.sliceTitle;
Page({

  data: {
      start:0,
      isEmpty:true,
  },
  onLoad: function (options) {
    var type = options.type;
    var title=options.title;
    var url=douban+type;
    http(url,this.handleData);
    wx.setNavigationBarTitle({
      title,
    });
    this.setData({
      type
    })
  },
  handleData(res){
    var title=res.data.title;
    var subjects=res.data.subjects;
    var movies=[];
    subjects.forEach(ele=>{
        var average=ele.rating.average;
        var stars=star(ele.rating.stars);
        var title=sliceTitle(ele.title);
        var imgUrl=ele.images.small;
        var id=ele.id;
        var temp={
            average,
            stars,
            title,
            imgUrl,
            id,
        }
        movies.push(temp);
    })
    if(this.data.isEmpty){
      this.setData({
        movies,
        title,
        isEmpty:false,
      })
    }else{
      this.setData({
        movies:this.data.movies.concat(movies)
      })
    }
    wx.hideLoading();
  },
  onReachBottom(){
    var type=this.data.type;
    this.data.start+=20;
    var start=this.data.start;
    var url=`${douban}${type}?start=${start}&count=20`;
    http(url,this.handleData);
    wx.showLoading({
      title:"加载数据"
    })
  }
})